﻿using LoveEatRabbit.API.Write.Applications.Commands.Users;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace LoveEatRabbit.API.Write.Applications.CommandHandlers.Users
{
    public class UserDeleteCommandHanddler : IRequestHandler<UserDeleteCommand, APIResult<int>>
    {
        private IBaseRespository<UserModel> userRep;

        public UserDeleteCommandHanddler(IBaseRespository<UserModel> userRep)
        {
            this.userRep = userRep;
        }

        public async Task<APIResult<int>> Handle(UserDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code=APIResultCode.删除成功;
            result.Message=APIResultCode.删除成功.ToString();

            var res=userRep.GetAll().FirstOrDefault(x=>x.UserId==request.UserId);

            res.IsDeleted=true;
            await userRep.UpdateAsync(res);


            return await Task.FromResult(result);
        }
    }
}
